package com.woniuxy.repay.controller;

import com.woniuxy.entity.RepaymentOverdue;
import com.woniuxy.repay.entity.PageBean;
import com.woniuxy.repay.service.RepaymentOverdueService;
import com.woniuxy.utils.ResponseResult;
import com.woniuxy.utils.ResultStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author shichen
 * @create 2021-11-13-11:47
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/repaymentOverdue")
public class RepaymentOverdueController {

    @Resource
    RepaymentOverdueService repaymentOverdueService;

    @GetMapping("/selectall/{pageIndex}")
    public ResponseResult selectAll(@PathVariable("pageIndex") Integer pageIndex, RepaymentOverdue repaymentOverdue) {
        log.debug("进入方法 selectAll");

        PageBean pageBean = repaymentOverdueService.selectByCondition(repaymentOverdue, pageIndex);


        log.debug("结果 : {} ", pageBean);
        ResponseResult responseResult =
                new ResponseResult(200, ResultStatus.HAVE_PERMS, "select ok", pageBean);

        return responseResult;
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody RepaymentOverdue repaymentOverdue) {
        log.debug("request : {}", repaymentOverdue);
        //=== 校验传过来的参数 start ===
        if (repaymentOverdue == null){
            return new ResponseResult(200,ResultStatus.NO_PERMS,"no request param",null);
        }
        //=== 校验传过来的参数 end   ===

        repaymentOverdueService.updateByCondition(repaymentOverdue);
        ResponseResult responseResult =
                new ResponseResult(200, ResultStatus.NO_PERMS, "update success", null);

        log.debug("response : {}", responseResult);
        return responseResult;
    }

    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody RepaymentOverdue repaymentOverdue) {
        log.debug("request : {}", repaymentOverdue);
        ResponseResult responseResult = null;
        //=== 校验传过来的参数 start ===
        if (repaymentOverdue != null) {

            try {
                repaymentOverdueService.insert(repaymentOverdue);
                responseResult = new ResponseResult(200, ResultStatus.NO_PERMS, "repaymentOverdue Insert Success", null);
            } catch (Exception e) {
                e.printStackTrace();
                responseResult = new ResponseResult(500, ResultStatus.NO_PERMS, "repaymentOverdue Insert Error", null);
            }
        }
        return responseResult;
    }
}
